package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.doman.pojo.Dept;
import com.itheima.doman.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     *  查询所有部门信息
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Dept> deptList = deptService.findAll();
        log.info("查询所有部门信息：{}",deptList);
        return Result.success(deptList);
    }

    /**
     *  根据id删除部门信息
     * @param id
     * @return
     */
    @Log
    @DeleteMapping
    public Result deleteById(@RequestParam Integer id){
        deptService.deleteById(id);
        log.info("删除部门信息：{}",id);
        return Result.success();
    }

    /**
     *  添加部门信息
     * @param dept
     * @return
     */
    @Log
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        log.info("添加部门信息：{}",dept);
        return Result.success();
    }

    /**
     *  根据id查询部门信息，用于回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Dept dept = deptService.findById(id);
        log.info("查询部门信息：{}",dept);
        return Result.success(dept);
    }

    /**
     *  修改部门信息     * @param dept
     * @return
     */
    @Log
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        deptService.updateDept(dept);
        log.info("修改部门信息：{}",dept);
        return Result.success();
    }

}
